{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Minimal Example pandapower\n",
    "\n",
    "\n",
    "## Creating a Power System\n",
    "\n",
    "We consider the following simple 3-bus example network as a minimal example:\n",
    "\n",
    "<img src=\"pics/3bus-system.png\" width=\"50%\">\n",
    "\n",
    "The above network can be created in pandapower as follows:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "from pandapower.create import (\n",
    "    create_empty_network,\n",
    "    create_bus,\n",
    "    create_ext_grid,\n",
    "    create_load,\n",
    "    create_line,\n",
    "    create_transformer,\n",
    "    create_switch\n",
    ")\n",
    "from pandapower.run import runpp\n",
    "import warnings\n",
    "warnings.simplefilter(action='ignore', category=FutureWarning)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "#create empty net\n",
    "net = create_empty_network()\n",
    "\n",
    "#create buses\n",
    "bus1 = create_bus(net, vn_kv=20., name=\"Bus 1\")\n",
    "bus2 = create_bus(net, vn_kv=0.4, name=\"Bus 2\")\n",
    "bus3 = create_bus(net, vn_kv=0.4, name=\"Bus 3\")\n",
    "\n",
    "#create bus elements\n",
    "create_ext_grid(net, bus=bus1, vm_pu=1.02, name=\"Grid Connection\")\n",
    "create_load(net, bus=bus3, p_mw=0.100, q_mvar=0.05, name=\"Load\")\n",
    "\n",
    "#create branch elements\n",
    "trafo = create_transformer(net, hv_bus=bus1, lv_bus=bus2, std_type=\"0.4 MVA 20/0.4 kV\", name=\"Trafo\")\n",
    "line = create_line(net, from_bus=bus2, to_bus=bus3, length_km=0.1, std_type=\"NAYY 4x50 SE\", name=\"Line\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Data Structure\n",
    "\n",
    "Each dataframe in a pandapower net object contains the information about one pandapower element, such as line, load transformer etc."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:21.552404Z",
     "start_time": "2025-10-20T06:46:21.526899Z"
    }
   },
   "outputs": [],
   "source": [
    "net.bus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:21.647192Z",
     "start_time": "2025-10-20T06:46:21.622236Z"
    }
   },
   "outputs": [],
   "source": [
    "net.line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:21.959355Z",
     "start_time": "2025-10-20T06:46:21.935921Z"
    }
   },
   "outputs": [],
   "source": [
    "net.trafo"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:22.116091Z",
     "start_time": "2025-10-20T06:46:22.091305Z"
    }
   },
   "outputs": [],
   "source": [
    "net.load"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Note that line and transformer are created with standard types, so thath the electric parameters of are automatically filled in from the standard type library."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Power Flow\n",
    "\n",
    "We now run a power flow:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:25.378613Z",
     "start_time": "2025-10-20T06:46:22.379537Z"
    }
   },
   "outputs": [],
   "source": [
    "runpp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And check out at the results for buses, lines an transformers:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:25.409460Z",
     "start_time": "2025-10-20T06:46:25.384935Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_bus"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:25.517812Z",
     "start_time": "2025-10-20T06:46:25.492385Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_line"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:25.929438Z",
     "start_time": "2025-10-20T06:46:25.913660Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_trafo"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Tap Changers\n",
    "\n",
    "We now lower the tap changer position, from position 0 to -1 and run another power flow:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:26.299773Z",
     "start_time": "2025-10-20T06:46:26.220149Z"
    }
   },
   "outputs": [],
   "source": [
    "net.trafo.at[trafo, \"tap_pos\"] = -1\n",
    "runpp(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Looking at the results shows that bus voltages at the low voltage side of the transformer have increased:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:26.501538Z",
     "start_time": "2025-10-20T06:46:26.479345Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### Switches\n",
    "\n",
    "We now create an open switch at the load bus:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:26.742197Z",
     "start_time": "2025-10-20T06:46:26.720159Z"
    }
   },
   "outputs": [],
   "source": [
    "create_switch(net, bus=bus3, element=line, et=\"l\", closed=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The open switch cuts the load bus from power supply:\n",
    "\n",
    "<img src=\"pics/3bus-system_switch.png\" width=\"8%\">"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This can be verified by running a power flow and inspecting the results. The voltage at bus 2 is given as NaN:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:26.929500Z",
     "start_time": "2025-10-20T06:46:26.870360Z"
    }
   },
   "outputs": [],
   "source": [
    "runpp(net)\n",
    "net.res_bus"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The load does not feed in:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:27.134630Z",
     "start_time": "2025-10-20T06:46:27.107080Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_load"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And the line is in open loop operation:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:27.519509Z",
     "start_time": "2025-10-20T06:46:27.498620Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_line"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Topological Analysis\n",
    "\n",
    "The structure of the network can also be directly analyzed with the topology package. It uses an interface to the NetworkX library for graph searches. There are some predefined search algorithms, such as searching for unsupplied buses:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:27.691425Z",
     "start_time": "2025-10-20T06:46:27.674098Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandapower.topology as top\n",
    "top.unsupplied_buses(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The package correctly determines that bus 2 is cut from power supply. When we close the switch, there are no unsupplied buses anymore:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:27.925659Z",
     "start_time": "2025-10-20T06:46:27.902570Z"
    }
   },
   "outputs": [],
   "source": [
    "net.switch.at[0, \"closed\"] = True\n",
    "top.unsupplied_buses(net)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Apart from predefined search functions, it is also possible to translate the pandapower network into a NetworkX graph and run searches directly on that graph.\n",
    "\n",
    "Suppose we want to find all buses that are on the same voltage level as the load bus. We then translate the grid into a graph but excluding the transformer:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:28.098688Z",
     "start_time": "2025-10-20T06:46:28.094169Z"
    }
   },
   "outputs": [],
   "source": [
    "mg = top.create_nxgraph(net, include_trafos=False)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "And search for all buses that are connected to the load bus in that graph:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:28.208636Z",
     "start_time": "2025-10-20T06:46:28.196212Z"
    }
   },
   "outputs": [],
   "source": [
    "list(top.connected_component(mg, 2))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "The graph search finds all buses that are on the same voltage level. Searches like these can be used for feeder identification and many more applications."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Short Circuit Analysis\n",
    "\n",
    "pandapower includes a short circuit module that complies with IEC 60909. To run a short circuit analysis, we need to define short circuit parameters for the external grid:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:28.291533Z",
     "start_time": "2025-10-20T06:46:28.269635Z"
    }
   },
   "outputs": [],
   "source": [
    "net.ext_grid[\"s_sc_max_mva\"] = 100\n",
    "net.ext_grid[\"rx_max\"] = 0.1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Now we can calculate short circuits. Here, we calculate a three phase short circuit current with a fault impedance of 2 Ohms:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:28.389280Z",
     "start_time": "2025-10-20T06:46:28.328571Z"
    }
   },
   "outputs": [],
   "source": [
    "import pandapower.shortcircuit as sc\n",
    "sc.calc_sc(net, case=\"max\", ip=True, r_fault_ohm=2.)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "Initial and peak short circuit currents are given for faults at all buses:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "ExecuteTime": {
     "end_time": "2025-10-20T06:46:28.420800Z",
     "start_time": "2025-10-20T06:46:28.403688Z"
    }
   },
   "outputs": [],
   "source": [
    "net.res_bus_sc"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "This concludes a short walkthrough of some pandapower features. More in-depth tutorials can be found in the pandapower documentation:\n",
    "https://www.pandapower.org/start/#interactive-tutorials-"
   ]
  }
 ],
 "metadata": {
  "anaconda-cloud": {},
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
